<template>
  <div>
    <div class="sousuo">
      <el-form :inline="true" :label-position="labelPosition">
        <el-form-item label="货物类型名称:">
          <el-input
            v-model="TruckAndGoods.name"
            placeholder="请输入货物类型名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="运输车型:">
          <el-select v-model="TruckAndGoods.dname" placeholder="请选择">
            <el-option label="大货车" value="100"></el-option>
            <el-option label="货车" value="101"></el-option>
            <el-option label="面包" value="102"></el-option>
            <el-option label="轿车" value="103"></el-option>
          </el-select>
        </el-form-item>
        <el-button type="danger" @click="show">搜索</el-button
        ><el-button @click="clear">重置</el-button>
      </el-form>
    </div>
    <div>
      <el-button type="danger" @click="dialogVisible = true"
        >+新增货物类型</el-button
      >
      <el-table
        :header-cell-style="{
          background: 'rgb(238, 238, 238)',
          color: 'black',
        }"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="货物类型编号"></el-table-column>
        <el-table-column prop="name" label="货物类型名称"></el-table-column>
        <el-table-column prop="dname" label="运输车型"></el-table-column>
        <el-table-column
          prop="defaultWeight"
          label="默认重量(kg)"
        ></el-table-column>
        <el-table-column prop="remark" label="说明"></el-table-column>
        <el-table-column prop="id" fixed="right" align="center" label="操作">
          <template v-slot="tt">
            <el-button
              size="mini"
              icon="el-icon-edit"
              @click="updategoods(tt.row, (dialogVisible1 = true))"
              type="warning"
              >修改</el-button
            >
            <el-button
              size="mini"
              icon="el-icon-edit"
              @click="remove(tt.row)"
              type="danger"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[2, 4, 6, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="新增货物类型" :visible.sync="dialogVisible" width="50%">
      <el-form :inline="true" :label-position="labelPosition">
        <el-form-item label="货物类型名称:">
          <el-input
            v-model="add.name"
            placeholder="请输入货物类型名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="运输车型:">
          <el-select v-model="add.dname" placeholder="请选择">
            <el-option label="大货车" value="100"></el-option>
            <el-option label="货车" value="101"></el-option>
            <el-option label="面包" value="102"></el-option>
            <el-option label="轿车" value="103"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认重量(kg):">
          <el-input
            v-model="add.defaultWeight"
            placeholder="请输入订单编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="默认体积:">
          <el-input
            v-model="add.defaultVolume"
            placeholder="请输入订单编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="说明:">
          <el-input
            v-model="add.remark"
            placeholder="请输入订单编号"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clear2(dialogVisible = false)">取消</el-button>
        <el-button
          type="primary"
          @click="addTruckAndGoods((dialogVisible = false))"
          >确定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog title="编辑货物类型" :visible.sync="dialogVisible1" width="40%">
      <el-form :inline="true" :label-position="labelPosition">
        <el-form-item label="货物类型名称:">
          <el-input
            v-model="update.name"
            placeholder="请输入货物类型名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="运输车型:">
          <el-select v-model="update.dname" placeholder="请选择">
            <el-option label="大货车" :value="100"></el-option>
            <el-option label="货车" :value="101"></el-option>
            <el-option label="面包" :value="102"></el-option>
            <el-option label="轿车" :value="103"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认重量(kg):">
          <el-input
            v-model="update.defaultWeight"
            placeholder="请输入订单编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="默认体积:">
          <el-input
            v-model="update.defaultVolume"
            placeholder="请输入订单编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="说明:">
          <el-input
            v-model="update.remark"
            placeholder="请输入订单编号"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button
          type="primary"
          @click="updategoodsandtruck((dialogVisible1 = false))"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ids: [],
      total: 0, //总条数
      tableData: [],
      page: 1, //当前页
      pageSize: 4, //每页的数据条数
      labelPosition: "left",
      dialogVisible: false,
      dialogVisible1: false,
      TruckAndGoods: {
        name: "",
        dname: "",
      },
      add: {
        name: "",
        dname: "",
        defaultWeight: "",
        defaultVolume: "",
        remark: "",
      },
      update: {},
    };
  },

  methods: {
    clear(){
      this.TruckAndGoods.name="";
      this.TruckAndGoods.dname="";
    },
    clear2(){
      this.add.name="";
      this.add.dname="";
      this.add.defaultWeight="";
      this.add.defaultVolume="";
      this.add.remark="";
},
    remove(row) {
      this.axios
        .post("/api/base/pd-goods-type/removeTruckAndGoods", row)
        .then((res) => {
          alert("删除成功");
          this.show();
        });
    },
    updategoodsandtruck() {
      this.axios
        .post("/api/base/pd-goods-type/updateTruckAndGoods", this.update)
        .then((res) => {
          alert("修改成功");
          this.show();
        });
    },
    addTruckAndGoods() {
      this.axios
        .post("/api/base/pd-goods-type/addTruckAndGoods", this.add)
        .then((res) => {
          this.add.name = "";
          this.add.dname = "";
          this.add.defaultWeight = "";
          this.add.defaultVolume = "";
          this.add.remark = "";
          alert("添加成功");
          this.show();
        });
    },
    updategoods(row) {
      this.update = row;
    },
    show() {
      this.axios
        .post("/api/base/pd-goods-type/findTruckAndGoods", {
          pageNum: this.page,
          pageSize: this.pageSize,
          search: this.TruckAndGoods,
        })
        .then((res) => {
          this.tableData = res.data.list;
          this.total = res.data.total;
          console.log(this.tableData);
        });
    },
    handleSelectionChange(val) {
      this.ids = val;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.show(this.page, this.pageSize);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.show(this.page, this.pageSize);
    },
  },
  created() {
    this.show();
  },
};
</script>

<style>
.sousuo {
  margin-top: 20px;
  margin-bottom: 20px;
}
</style>
